package com.hiquety.csv2sql.tool;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.net.URL;

/**
 * @Author: hetong
 * @Date: 2019-10-31 20:02
 * @Version 1.0
 */
public class FxmlUtils {

    public static FxmlBean loadFxml(String relativePath) {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            URL url = fxmlLoader.getClassLoader().getResource(relativePath);
            fxmlLoader.setLocation(url);

            Pane pane = fxmlLoader.load();
            Scene scene = new Scene(pane);
            Stage stage = new Stage();
            stage.setScene(scene);
            Object controller = fxmlLoader.getController();

            FxmlBean bean = new FxmlBean();
            bean.setPane(pane);
            bean.setScene(scene);
            bean.setStage(stage);
            bean.setController(controller);
            bean.setFxmlLoader(fxmlLoader);

            return bean;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
